from constants import *
import pygame,random,time
from ds import DS
from task import Task

#####################################################
#                                                   #
#  Queue class:                                     # 
#                                                   #
#  The Queue class implements a queue that can      #
#  be visualized through the display function.      #
#                                                   #
#  It can be used to represent FIFO or LIFO         #
#  data structures                                  #
#                                                   # 
#  Author: Felix Rohrer                             #     
#  Course: CS MET 575                               #
#  Year:   2011                                     #
#                                                   #
#####################################################

class Queue(DS):

  def __init__(self, numOfElem, pos, name='',drawAreaDim=(700,600)):
    DS.__init__(self,numOfElem, pos, name, drawAreaDim)
    self.DSType = 'QUEUE'

  def display(self, canvas):
    # draw rects that belong currently to the queue
    DS.display(self,canvas,bgColor=QUEUE_BG_COLOR)
  
    # draw name into task
    canvas.blit(self.textFont.render(self.name, True, (0,0,255)),(self.pos[0]+2, self.pos[1]+self.size[1]+2))

  # push task to this data structure
  # returns True if task successfully added, False otherwise
  def push(self,task):
    if self.tasks[self.tail] is None:
      # add this task to the queue
      task.setDS(self)
      self.tasks[self.tail] = task
      task.setPos((self.pos[0]+10+50*self.tail,self.pos[1]+10))
      self.incTail()
      return True
    else:
      return False

  # returns task that has been removed from this data structure
  def pop(self):
    if self.tail >= 0:
      task = self.tasks[0]
      task.setDS(None)
      self.tasks[0] = None
      newPos = 0
      for t in range(1,len(self.tasks)):
        if self.tasks[t] is not None:
          self.tasks[t].setPos((self.pos[0]+10+50*newPos,self.pos[1]+10))
          self.tasks[t-1] = self.tasks[t]
          self.tasks[t] = None
          newPos += 1
      self.decTail()
      return task
    return None

  # clear all allocated elements for this data structure
  def empty(self):
    DS.empty(self)
    self.iterPtr = 0
    self.tasks = [None] * self.numOfElem
